const { DataTypes } = require("sequelize")
const sequelize = require("../config/database")

const Household = sequelize.define(
  "household",
  {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    unit_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: "unit",
        key: "id",
      },
      onDelete: "SET NULL",
      onUpdate: "CASCADE",
    },
    building_name: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: "楼栋名称",
    },
    unit_number: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: "单元号",
    },
    floor_num: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: "楼层数",
    },
    min_height: {
      type: DataTypes.FLOAT,
      allowNull: true,
    },
    max_height: {
      type: DataTypes.FLOAT,
      allowNull: true,
    },
    owner_name: {
      type: DataTypes.STRING(50),
      allowNull: true,
    },
    id_card: {
      type: DataTypes.STRING(50),
      allowNull: true,
    },
    phone_num: {
      type: DataTypes.STRING(50),
      allowNull: true,
    },
    owner_sex: {
      type: DataTypes.STRING(6),
      allowNull: true,
      comment: "性别 1男  2女",
    },
    native_place: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: "籍贯",
    },
    owner_img: {
      type: DataTypes.STRING(255),
      allowNull: true,
    },
    water_fee: {
      type: DataTypes.FLOAT,
      allowNull: true,
      defaultValue: 0,
      comment: "水费",
    },
    electric_fee: {
      type: DataTypes.FLOAT,
      allowNull: true,
      defaultValue: 0,
      comment: "电费",
    },
    property_fee: {
      type: DataTypes.FLOAT,
      allowNull: true,
      defaultValue: 0,
      comment: "物业费",
    },
    is_paid: {
      type: DataTypes.BOOLEAN,
      allowNull: true,
      defaultValue: false,
      comment: "是否缴费完成",
    },
    house_type: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: "房屋类型",
    },
    built_area: {
      type: DataTypes.FLOAT,
      allowNull: true,
      comment: "建筑面积",
    },
    orientation: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: "房屋朝向",
    },
    property_type: {
      type: DataTypes.SMALLINT,
      allowNull: true,
      comment: "物业类型 1居民物业 2商业物业 3工业物业 4其他",
    },
    house_address: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: "房间号码",
    },
    house_img: {
      type: DataTypes.STRING(255),
      allowNull: true,
      comment: "房屋图片",
    },
    create_at: {
      type: DataTypes.DATE,
      defaultValue: DataTypes.NOW,
    },
    update_at: {
      type: DataTypes.DATE,
      defaultValue: DataTypes.NOW,
    },
    delete_at: {
      type: DataTypes.DATE,
      allowNull: true,
    },
  },
  {
    timestamps: true,
    createdAt: "create_at",
    updatedAt: "update_at",
    deletedAt: "delete_at",
    tableName: "household",
    hooks: {
      beforeUpdate: (instance) => {
        instance.update_at = new Date()
      },
    },
  }
)

module.exports = Household
